local zhilun = fk.CreateSkill{
  name = "hanqing__zhilun",
}

Fk:loadTranslationTable{
  ["hanqing__zhilun"] = "质论",
  [":hanqing__zhilun"] = "出牌阶段限三次，你可以与一名角色拼点，赢的角色可以获得或移动没赢的角色区域内的一张牌。",

  ["#hanqing__zhilun"] = "质论：与一名角色拼点，赢者可以获得或移动对方区域内的一张牌",
  ["#hanqing__zhilun-invoke"] = "质论：是否获得或移动 %dest 区域内的一张牌？",
  ["#hanqing__zhilun-choose"] = "质论：选择一名角色将此牌移至对应区域，或点“取消”你获得之",
}

zhilun:addEffect("active", {
  anim_type = "control",
  prompt = "#hanqing__zhilun",
  card_num = 0,
  target_num = 1,
  times = function(self, player)
    return player.phase == Player.Play and
      3 - player:usedSkillTimes(zhilun.name, Player.HistoryPhase) or -1
  end,
  can_use = function(self, player)
    return player:usedSkillTimes(zhilun.name, Player.HistoryPhase) < 3
  end,
  card_filter = Util.FalseFunc,
  target_filter = function(self, player, to_select, selected, selected_cards)
    return #selected == 0 and player:canPindian(to_select)
  end,
  on_use = function(self, room, effect)
    local player = effect.from
    local target = effect.tos[1]
    local pindian = player:pindian({target}, zhilun.name)
    if player.dead or target.dead then return end
    local winner = pindian.results[target].winner
    if winner then
      local loser = winner == player and target or player
      if loser:isAllNude() or
        not room:askToSkillInvoke(winner, {
          skill_name = zhilun.name,
          prompt = "#hanqing__zhilun-invoke::"..loser.id,
        }) then return end
      local card = room:askToChooseCard(winner, {
        target = loser,
        flag = "hej",
        skill_name = zhilun.name,
      })
      local targets
      if table.contains(loser:getCardIds("ej"), card) then
        targets = table.filter(room:getOtherPlayers(loser, false), function (p)
          return loser:canMoveCardInBoardTo(p, card)
        end)
      else
        targets = room.alive_players
      end
      if #targets == 0 then
        room:moveCardTo(card, Card.PlayerHand, winner, fk.ReasonPrey, zhilun.name, nil, false, winner)
      else
        local to = room:askToChoosePlayers(winner, {
          min_num = 1,
          max_num = 1,
          targets = targets,
          skill_name = zhilun.name,
          prompt = "#hanqing__zhilun-choose",
          cancelable = true,
        })
        if #to > 0 then
          to = to[1]
          card = loser:getVirualEquip(card) or Fk:getCardById(card)
          room:moveCardTo(card, room:getCardArea(card), to, fk.ReasonJustMove, zhilun.name, nil, false, winner)
        else
          room:moveCardTo(card, Card.PlayerHand, winner, fk.ReasonPrey, zhilun.name, nil, false, winner)
        end
      end
    end
  end,
})

return zhilun
